package com.ruoyi.system.controller;


import java.util.Date;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.system.domain.*;

import com.ruoyi.system.service.*;
import com.ruoyi.system.util.MinioUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.SneakyThrows;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 设备管理Controller
 *
 * @author ruoyi
 * @date 2023-06-29
 */
@Api("设备管理")
@RestController
@RequestMapping("/system/manage")
public class EquipmentManageController extends BaseController
{
    @Autowired
    private IEquipmentManageService equipmentManageService;
    @Autowired
    private ICoverService coverService;
    @Autowired
    private SysUsersService sysUsersService;
    @Autowired
    private JgEquipmentService jgEquipmentService;
    @Autowired
    private DevGroupService devGroupService;
    @Autowired
    private MinioUtil minioUtil;


    /**
     * 查询设备管理列表
     */
    @ApiOperation("查询设备管理列表")
    @PreAuthorize("@ss.hasPermi('system:manage:list')")
    @GetMapping("/list")
    public TableDataInfo list(EquipmentManage equipmentManage)
    {
        startPage();
        List<EquipmentManage> list = equipmentManageService.selectEquipmentManageList(equipmentManage);
        return getDataTable(list);
    }

    /**
     * 导出设备管理列表
     */
    @PreAuthorize("@ss.hasPermi('system:manage:export')")
    @Log(title = "设备管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, EquipmentManage equipmentManage)
    {
        List<EquipmentManage> list = equipmentManageService.selectEquipmentManageList(equipmentManage);
        ExcelUtil<EquipmentManage> util = new ExcelUtil<EquipmentManage>(EquipmentManage.class);
        util.exportExcel(response, list, "设备管理数据");
    }

    /**
     * 获取设备管理详细信息
     */
    @ApiOperation("获取设备管理详细信息")
    @PreAuthorize("@ss.hasPermi('system:manage:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(equipmentManageService.selectEquipmentManageById(id));
    }
    /**
     * 接收图片
     */
    String url="";
    @SneakyThrows
    @ApiOperation("接收前端传递的图片")
    @PreAuthorize("@ss.hasPermi('system:manage:upload')")
    @Log(title = "设备管理", businessType = BusinessType.INSERT)
    @PostMapping("/upload")
    public String upload(@RequestPart("file") MultipartFile file)
    {
        if (file!=null){
            url=minioUtil.minioUpload(file);
        }
        return url;
    }
    /**
     * 新增设备管理
     */
    @ApiOperation("新增设备管理")
    @PreAuthorize("@ss.hasPermi('system:manage:add')")
    @Log(title = "设备管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody EquipmentManage equipmentManage)
    {
        Integer coverId  = coverService.selectCoverByCode(equipmentManage.getCode());
        equipmentManage.setCoverId(Long.valueOf(coverId));
        equipmentManage.setLatestTime(new Date());
        equipmentManage.setStatus(1L);
        equipmentManage.setImg(url);
        jgEquipmentService.insertIMEI(equipmentManage);
        return toAjax(equipmentManageService.insertEquipmentManage(equipmentManage));

    }

    /**
     * 修改设备管理
     */
    @ApiOperation("修改设备管理")
    @PreAuthorize("@ss.hasPermi('system:manage:edit')")
    @Log(title = "设备管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody EquipmentManage equipmentManage)
    {
        equipmentManage.setImg(url);
        return toAjax(equipmentManageService.updateEquipmentManage(equipmentManage));
    }

    /**
     * 删除设备管理
     */
    @ApiOperation("删除设备管理")
    @PreAuthorize("@ss.hasPermi('system:manage:remove')")
    @Log(title = "设备管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(equipmentManageService.deleteEquipmentManageByIds(ids));
    }

    /**
     * 查询用户表
     */
    @ApiOperation("查询用户表")
    @PreAuthorize("@ss.hasPermi('system:manage:userlist')")
    @GetMapping("/userlist")
    public TableDataInfo userList(SysUsers sysUser){
        List<SysUsers> list = sysUsersService.selectSysUserList(sysUser);
        return getDataTable(list);
    }

    /**
     * 查询所有井盖编号
     * @param cover
     * @return
     */
    @ApiOperation("查询井盖编号")
    @PreAuthorize("@ss.hasPermi('system:manage:listCoverNum')")
    @GetMapping("/listCoverNum")
    public TableDataInfo listCoverNum(Cover cover){
        List<SysUsers> list = coverService.listCoverNum(cover);
        return getDataTable(list);
    }

    /**
     * 查询设备类型表的设备名称
     * @param
     * @return
     */
    @GetMapping("/listDeviceName")
    public TableDataInfo listDeviceName(DevGroup devGroup){
        List<DevGroup> list = devGroupService.ListDevType(devGroup);
        return getDataTable(list);
    }
    /**
     * 根据部门id查询设备号
     */
    @GetMapping("/ListIMEI")
    public TableDataInfo listDeviceName(Integer deptid){
        List<EquipmentManage> list = equipmentManageService.ListIMEI(deptid);
        return getDataTable(list);
    }
}
